package com.carlinfo.demo.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IAKingDao;
import com.carlinfo.demo.pojo.AKing;

public class AKingDaoImpl extends BaseDaoImpl<AKing> implements IAKingDao
{
	public AKingDaoImpl()
	{
		AKing king = new AKing();
		this.baseObj = king;
	}
	@Override
	public int saveOneDao(AKing king)
	{
		String sql = "insert into a_king values (null,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> list = new ArrayList<Object>();
		list.add(king.getDynastyId());
		list.add(king.getName());
		list.add(king.getAge());
		list.add(king.getHeight());
		list.add(king.getMiaoHao());
		list.add(king.getContext());
		list.add(king.getStatus());
		list.add(king.getNianHao());
		list.add(king.getCreateTime());
		list.add(king.getUpdateTime());
		list.add(king.getPubTime());
		Map<String, Object> condMap = new HashMap<String, Object>();

		int res = this.excJDBC(sql, list, condMap);
		return res;
	}

	@Override
	public List<AKing> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from a_king where 1=1 ";
		/*
		 * 关键字: select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%'); 状态 : select * from a_dynasty where status = 1 ; 时间 :
		 * select * from a_dynasty where (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 关键字+状态: select * from a_dynasty where (name like
		 * '%0%' or capital like '%0%' or context like '%0%') and status = 1 ; 关键字+时间:
		 * select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%') and (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 时间+状态: select * from a_dynasty where status = 1 and
		 * (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
		 * 关键字+状态+时间: select * from a_dynasty where (name like '%0%' or capital like
		 * '%0%' or context like '%0%') and status = 1 and (pubTime >= '2018-09-18
		 * 00:00:00' and pubTime < '2018-09-19 10:00:00'); 啥也不选: select * from
		 * a_dysnasty ;
		 */

		/* 根据条件来组装sql语句 */
		if (condMap.get("keyWord") != null && !"".equalsIgnoreCase(condMap.get("keyWord") + ""))
		{
			sql += " and (name like ? or miaoHao like ? or nianHao like ?) ";
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			list.add(condMap.get("status"));
		}
		if (condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			sql += " and  (pubTime >= ? and pubTime < ?) ";
			list.add(condMap.get("stDate"));
			list.add(condMap.get("edDate"));
		}

		/**
		 * 设置排序方式
		 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc ";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc name desc ";
		} else
		{
			sql += " order by pubTime desc ";
		}

		if (pageInfoUtil != null)
		{
			return this.findListByPage(pageInfoUtil, sql, list);
		}
		return this.findList(sql, list);
	}

	@Override
	public AKing selectOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from a_king ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ? limit 1;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("name") + "")) && condMap.get("name") != null)
		{
			sql += "where name = ? limit 1;";
			list.add(condMap.get("name"));
		}
		return this.findONeJDBC(sql, list);
	}

	@Override
	public int updateOneDao(AKing king)
	{
		String sql = "update a_king set name=?,height=?,dynastyId=?,age=?,status=?,context=?"
				+ ",miaoHao=?,createTime=?,pubTime=?,updateTime=?,nianHao=? where id =?;";
		List<Object> list = new ArrayList<Object>();
		list.add(king.getName());
		list.add(king.getHeight());
		list.add(king.getDynastyId());
		list.add(king.getAge());
		list.add(king.getStatus());
		list.add(king.getContext());
		list.add(king.getMiaoHao());
		list.add(king.getCreateTime());
		list.add(king.getPubTime());
		list.add(king.getUpdateTime());
		list.add(king.getNianHao());
		list.add(king.getId());
		return this.excJDBC(sql, list);
	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "delete from a_king ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ?;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("name") + "")) && condMap.get("name") != null)
		{
			sql += "where name = ? ;";
			list.add(condMap.get("name"));
		}
		return this.excJDBC(sql, list);
	}

	
	protected AKing convertFromDb_source(ResultSet rs) throws SQLException
	{
		AKing king = new AKing();
		king.setId(rs.getInt("id"));
		king.setAge(rs.getInt("age"));
		king.setContext(rs.getString("context"));
		king.setCreateTime(rs.getTimestamp("createTime"));
		king.setDynastyId(rs.getInt("dynastyId"));
		king.setHeight(rs.getDouble("height"));
		king.setUpdateTime(rs.getTimestamp("updateTime"));
		king.setPubTime(rs.getTimestamp("pubTime"));
		king.setMiaoHao(rs.getString("miaoHao"));
		king.setNianHao(rs.getString("nianHao"));
		king.setStatus(rs.getByte("status"));
		king.setName(rs.getString("name"));
		return king;
	}

}
